/*
--------------------------------------------------------------------------------
%include "/projects/hsieh_project/code_0_general/m_rank_by_var.sas" /source2;
%m_rank_by_var(ds= , var_by0= , var_by1= , var_tie= , var_rank= );

ds = data set in which the rank is created
var_by0 = rank by group
var_by1 = varibale used to create rank
var_tie = variable used to break ties
var_rank = rank variable
--------------------------------------------------------------------------------
*/

%macro m_rank_by_var(ds= , var_by0= , var_by1= , var_tie= , var_rank= );

proc sort data=&ds.;
  by &var_by0. &var_by1. &var_tie.;
run;

%Let n_var=%sysfunc(countW(%bquote(&var_by0.)));
%Let var_bys=%scan(%bquote(&var_by0.), &n_var.);

data &ds.;
  set &ds.;
  by &var_by0.;
  if first.&var_bys. then &var_rank.=0;
  &var_rank.+1;
run;

%mend;

/* End of SAS file */
